package com.kbt.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.kbt.activity.GoodRoadInfoActivity;
import com.kbt.activity.KuaiDiActivity;
import com.kbt.activity.OrderJudgeActivity;
import com.kbt.activity.R;
import com.kbt.listener.ClickActStartActForResultListener;
import com.kbt.listener.ClickConfirmOrderListener;
import com.kbt.model.data.GoodForOrderInfoModel;
import com.kbt.model.data.OrderBean;
import com.kbt.model.data.OrderGoodsListBean;
import com.kbt.model.data.OrderInfoModel;
import com.kbt.model.holder.OrderInfoHolder;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.RestListViewSizeTool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhgk
 * @package: com.kbt.adapter
 * @TODO: 全部订单的适配器
 * @data: 2015/8/20 9:28
 * @version: V1.0
 */
public class OrderWaitReviveAdapter extends OrderBaseAdapter {

    public OrderWaitReviveAdapter(Activity context, List<OrderBean> orderList) {
        super(context, orderList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderInfoHolder itemHolder;

        if (convertView == null) {
            itemHolder = new OrderInfoHolder();

            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.good_order_item, null);

            itemHolder.setOrderNoView((TextView) convertView.findViewById(R.id.orderNo));
            itemHolder.setOrderCreateTimeView((TextView) convertView.findViewById(R.id.createTime));

            itemHolder.setGoodListView((ListView) convertView.findViewById(R.id.goodListForOrder));

            itemHolder.setOrderStatus((TextView) convertView.findViewById(R.id.orderStatus));

            itemHolder.setOrderCount((TextView) convertView.findViewById(R.id.orderCount));
            itemHolder.setOrderFee((TextView) convertView.findViewById(R.id.totalFeel));

            itemHolder.setBtnA((Button) convertView.findViewById(R.id.orderBtn_A));
            itemHolder.setBtnB((Button) convertView.findViewById(R.id.orderBtn_B));


            convertView.setTag(itemHolder);
        } else {
            itemHolder = (OrderInfoHolder) convertView.getTag();
        }

        final OrderBean orderInfo = orderList.get(position);
        itemHolder.getOrderNoView().setText("订单号：" + orderInfo.getOrder_no());

        itemHolder.getOrderCreateTimeView().setText(orderInfo.getOrder_create_time());

        //订单商品列表
        List<OrderGoodsListBean> goodList = orderInfo.getGoods_list();
        GoodForOrderItemAdapter itemAdapter = new GoodForOrderItemAdapter(context, goodList);
        itemHolder.getGoodListView().setAdapter(itemAdapter);
        RestListViewSizeTool.setListViewHeightBasedOnChildren(itemHolder.getGoodListView());

        if(orderInfo.getOrder_status().equals("3")) {
            itemHolder.getOrderStatus().setText("待收货");
        }

        itemHolder.getOrderCount().setText("共计 " + orderInfo.getGoods_count() + " 件");
        itemHolder.getOrderFee().setText(String.valueOf(orderInfo.getOrder_real_je()));
//        BtnSetting.setBtnBackground(itemHolder.getBtnA(), 2, Color.RED, Color.RED, 5);

        itemHolder.getBtnA().setTextColor(Color.WHITE);
//        BtnSetting.setBtnBackground(itemHolder.getBtnB(), 2, Color.RED, Color.RED, 5);

             itemHolder.getBtnA().setText("查看物流");
             itemHolder.getBtnB().setText("确认收货");
            //临时增加事件
//            itemHolder.getBtnA().setOnClickListener(new ClickActStartActForResultListener(context,
//                    GoodRoadInfoActivity.class, 1, null));
            itemHolder.getBtnA().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent  = new Intent(context,KuaiDiActivity.class);
                    intent.putExtra("bm",orderInfo.getOrder_kd_bm());
                    intent.putExtra("dh",orderInfo.getOrder_kd_dh());
                    intent.putExtra("kd_name",orderInfo.getOrder_kd_name());
                    context.startActivity(intent);
                }
            });
            itemHolder.getBtnB().setOnClickListener(new ClickConfirmOrderListener(context, convertView,
                    OrderWaitReviveAdapter.this, orderList, position, StaticData.POPWINDOW_CONFIRM_ORDER));


        itemHolder.getBtnB().setTextColor(Color.WHITE);


        return super.getView(position, convertView, parent);
    }
}
